 

function announcementCtrl($scope, $http) {
  'ngInject';

  // 发布公告单位
  this.isMandatory = false;

  this.msg = '说明：公告附件仅可在网页端预览及下载。';

  this.getNoticeUnit = () => {
    $http.get(`${SERVER_CONTEXT}/api/user/app/notice/noticeUnit`)
      .then((resp) => {
        this.unit = resp.data;
      });
  }

  // textarea change事件
  this.checkText = () => {
    if(this.announcementInfo.context.length > '5000'){
      this.reasonAddition = true;
    }else {
      this.reasonAddition = false;
    }
  }

  // 上传附件 用于接收附件token 多个附件以逗号分隔
  this.profilesToken = [];

  this.save = (param) => {
  	// 修改表单是否提交过状态
  	param.$submitted = true;

  	// 表单校验
  	if (param.$invalid) {
  	  return false;
  	}

    var orgIds = "";

  	// 原因分析
  	this.unit.forEach(function(item,index){
  	  if (item.active) {
  	    orgIds = orgIds ? orgIds + ',' + item.id : item.id;
  	  }   
  	});

  	this.announcementInfo.orgIds = orgIds;

    if (this.announcementInfo.orgIds == "") {
      this.isMandatory = true;
      return false;
    }

    this.announcementInfo.attachment = this.profilesToken.map(item => item.id).join(',');   
    
  	$http.post(`${SERVER_CONTEXT}/api/user/app/notice/insertNoticeInfo`,
  	    this.announcementInfo
  	    )
  	  .then((resp) => {
  	    if (resp.data.success) {
  	      layer.msg(resp.data.message);
          $state.reload();
          $state.go('projectNotice');
  	    } else {
  	      layer.alert(resp.data.message);
  	    }     
  	  });

  }

  this.initPage = () => {
    // 获取发布单位信息
    this.getNoticeUnit();
  }

  this.initPage();

}

export default announcementCtrl;